package com.java.zhuiyun.api.member_manager.service.impl;

import com.java.zhuiyun.api.back_course.model.BackCourseModel;
import com.java.zhuiyun.api.member_manager.entity.MemberType;
import com.java.zhuiyun.api.member_manager.mapper.MemberTypeMapper;
import com.java.zhuiyun.api.member_manager.model.MemberTypeModel;
import com.java.zhuiyun.api.member_manager.service.MemberTypeService;
import com.java.zhuiyun.common.response.CommonResult;
import com.java.zhuiyun.util.UploadFileUtils;
import com.java.zhuiyun.util.pageUtil.MybatisPageHelper;
import com.java.zhuiyun.util.pageUtil.PageRequest;
import com.java.zhuiyun.util.pageUtil.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

/**
 * @ClassName MemberTypeServiceImpl
 * @Description TODO 会员类型管理接口实现
 * @Author WX
 * @Date 2023/8/14 19:08
 **/
@Service
public class MemberTypeServiceImpl implements MemberTypeService {
    @Autowired
    private MemberTypeMapper memberTypeMapper;

    /*
     * @Author WX
     * @Description TODO 查询所有类型 全查
     * @Date 2023/8/14 19:50
     * @Return
     * @param MemberType,PageRequest
     */
    @Override
    public CommonResult selectMemberType(MemberType memberType, PageRequest pageRequest) {
        MybatisPageHelper.startPage(pageRequest);
        List<MemberTypeModel> backCourseModels = memberTypeMapper.selectMemberType(memberType);
        PageResult pageResult = MybatisPageHelper.getPageResult(backCourseModels);
        return CommonResult.ok(pageResult);
    }

    /*
     * @Author WX
     * @Description TODO 查询一条类型数据
     * @Date 2023/8/14 19:22
     * @Return
     * @param id
     */
    @Override
    public MemberType fingById(Integer id) {
        MemberType memberType = memberTypeMapper.fingById(id);
        return memberType;
    }

    /*
     * @Author WX
     * @Description TODO 修改状态
     * @Date 2023/8/14 19:22
     * @Return
     * @param memberType
     */
    @Override
    public int updateModify(MemberType memberType) {
            return memberTypeMapper.updateModify(memberType);

    }

    /*
     * @Author WX
     * @Description TODO 修改类型
     * @Date 2023/8/14 19:22
     * @Return
     * @param memberType
     */
    @Override
    public int updateMemberType(MemberType memberType,MultipartFile file) {
        if(file !=null){memberType.setMemberUrl(getImage(file));}
        memberType.setUpdateTime(new Date());
        return memberTypeMapper.updateMemberType(memberType);
    }

    /*
     * @Author WX
     * @Description TODO 添加会员类型
     * @Date 2023/8/14 19:22
     * @Return
     * @param memberType
     */
    @Override
    public int addMemberType(MemberType memberType, MultipartFile file) {
        String s = UploadFileUtils.uploadFile(file);
        System.out.println(s);
        memberType.setMemberUrl(s);
        memberType.setCreateTime(new Date());
        memberType.setUpdateTime(new Date());
        return memberTypeMapper.addMemberType(memberType);
    }

    /*
     * @Author WX
     * @Description TODO 删除类型
     * @Date 2023/8/14 19:22
     * @Return
     * @param id
     */
    @Override
    public int deleteById(Integer id) {
        return memberTypeMapper.deleteById(id);
    }

    /*
     * @Author WX
     * @Description TODO 查询所有类型 模糊查
     * @Date 2023/8/14 19:50
     * @Return
     * @param MemberType,PageRequest
     */
    @Override
    public CommonResult selectMoHuMemberType(MemberType memberType, PageRequest pageRequest) {
        MybatisPageHelper.startPage(pageRequest);
        List<MemberTypeModel> backCourseModels = memberTypeMapper.selectMoHuMemberType(memberType);
        PageResult pageResult = MybatisPageHelper.getPageResult(backCourseModels);
        return CommonResult.ok(pageResult);
    }

    /*
     * @Author WX
     * @Description 查询会员类型
     * @Date 2023/8/30 10:09
     * @Return
     * @param null
     */
    @Override
    public List<MemberType> getMemberList() {
        return memberTypeMapper.getMemberList();
    }
    /*
     * @Author WX
     * @Description pc端查询会员类型
     * @Date 2023/8/30 10:09
     * @Return
     * @param null
     */
    @Override
    public List<MemberType> selectMemberList() {
        return memberTypeMapper.selectMemberList();
    }

    /*
    * @Author WX
    * @Description TODO 图片上传至服务器
    * @Date 2023/8/16 8:43
    * @Return
    * @param
    */
    public static String getImage(MultipartFile file){
        String s= UploadFileUtils.uploadFile(file);
        if (s != ""){
            return s;
        }
        return null;
    }
}
